package project;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Calendar;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class Conn {
	public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
	private Connection connect = null;
	private Statement statement = null;
	private Connection conn = null;
	private ResultSet resultSet = null;
	private int val_noua = 0;
	private int contor1 = 0;
	private int contor2 = 0;
	public List<String> date = new ArrayList<String>();
	public List<Integer> valori = new ArrayList<Integer>();

	// private PreparedStatement preparedStatement = null;
	public void conectare() {
		try {
			String userName = "root";
			String password = "gravitant";
			String url = "jdbc:mysql://localhost/date";
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection(url, userName, password);
		} catch (Exception e) {
			System.err.println("Cannot connect to database server");
		}
	}

	public void deconectare() {
		try {

			conn.close();

		} catch (Exception e) {
			System.err.println("Cannot connect to database server");
		}
	}

	public void IntoarceDate() {
		List<String> datetemp = new ArrayList<String>();
		Statement s;
		try {
			s = conn.createStatement();
			s.executeQuery("select * from (select * from date_receptionate order by id desc limit 30)as aa order by id;");
			ResultSet rs = s.getResultSet();
			int count = 0;
			while (rs.next()) {
				int idVal = rs.getInt("id");
				String catVal = rs.getString("data");
				++count;
				if (contor1 < count) {
					contor1 = count;
					date.add(catVal);
				}
			}
			rs.close();
			s.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void IntoarceValori() {
		List<Integer> valoritemp = new ArrayList<Integer>();
		Statement s;
		try {
			s = conn.createStatement();
			s.executeQuery("select * from (select * from date_receptionate order by id desc limit 30)as aa order by id;");
			ResultSet rs = s.getResultSet();
			int count = 0;
			while (rs.next()) {
				int idVal = rs.getInt("id");
				int nameVal = rs.getInt("valoare");
				++count;
				if (contor2 < count) {
					contor2 = count;
					valori.add(nameVal);
				}
			}
			rs.close();
			s.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
